package com.shg.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shg.admin.bean.User;
import com.shg.admin.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TableController {

    private final UserService userService;

    public TableController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/basic_table")
    public String basic_table(@RequestParam(value = "a") int a) {

        // 模拟服务端出错
        // int i = 1/0;
        return "table/basic_table";
    }

    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {

        // List<User> users = Arrays.asList(new User("张三", 18, "18@qq.com"),
        //         new User("李四", 19, "19@qq.com"),
        //         new User("王五", 20, "20@qq.com"),
        //         new User("赵六 ", 21, "21@qq.com"));
        // model.addAttribute("users", users);

        // 学习第二种异常处理方式时写的测试内容 @ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户数量太多") 
        // if(users.size() > 3){
        //    throw new UserTooManyException();
        //}

        // 查询全部用户数据
        // List<User> userList = userService.list();
        // model.addAttribute("users", userList);

        Page<User> page = new Page<>(pn, 2);
        Page<User> userPage = userService.page(page, null);
        long current = userPage.getCurrent();   // 当前第几页
        long pages = userPage.getPages();       // 总共多少页
        long total = userPage.getTotal();       // 总共多少条记录数
        List<User> records = userPage.getRecords(); // 数据信息
        model.addAttribute("users", userPage);
        return "table/dynamic_table";
    }


    @GetMapping("/responsive_table")
    public String responsive_table() {
        return "table/responsive_table";
    }

    @GetMapping("/editable_table")
    public String editable_table() {
        return "table/editable_table";
    }
}
